<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="2"
        :formType="type"
        labelWidth="90"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">确定</AjaxButton>
    </div>
  </div>
</template>

<script>
import {
  addTestDriveCars,
  editTestDriveCars,
  getBrandList,
  getSeriesList,
  getModelList,
  getPackageList
 } from '@/api/potenCus/basedata/searchTestDriveCars';
import { dictionary } from '@/utils/dictMixin';
import AjaxButton from '@/components/AjaxButton';
import { phone } from '@/utils/validate'

export default {
  name: 'addContact',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    },
    contantypeList: ''
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList()
    };
  },
  created() {
    this.init();
    console.log(111,this.contantypeList)
  },
  methods: {
    init() {
      let arr = this.createDictList('1534');
      let newarr = [];
      arr.map(x=>{
        if (x.value !== 15341008) {
          newarr.push(x)
        }
      })
      this.formList.find(item=>item.fieldName==='contactRelation').itemList = newarr;

      let {rowData} = this
      console.log('编辑联系人', rowData, this.type)
      if(this.type === 'edit'){
        this.formList.forEach( x =>{
          x.initialValue = rowData[x.fieldName]
        })
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '联系人姓名',
          fieldName: 'contactName',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入联系人姓名', trigger: 'blur' }],
        },
        {
          type: 'SELECT',
          label: '性别',
          fieldName: 'gender',
          placeholder: '请选择',
          itemList: this.createDictList('1002'),
        },
        {
          type: 'INPUT',
          label: '手机号码',
          fieldName: 'contactorPhone',
          maxlength: 11,
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入手机号码', trigger: 'blur' },
                  { trigger: 'blur', validator: phone }],
        },
        {
          type: 'SELECT',
          label: '联系人类型',
          fieldName: 'contactType',
          placeholder: '请选择',
          itemList: this.contantypeList,
          rules: [{ required: true, message: '请选择联系人类型', trigger: 'blur' }],
        },
        {
          type: 'SELECT',
          label: '关系',
          fieldName: 'contactRelation',
          placeholder: '请选择',
          itemList: [],
          initialValue : '',
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'contactRemark',
          placeholder: '请输入',
        },

      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    // TODO:
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        // console.log("联系人保存",this.formList)
        let params={
          contactName: this.formList[0].initialValue,
          gender: this.formList[1].initialValue,
          contactorPhone: this.formList[2].initialValue,
          contactType: this.formList[3].initialValue,
          contactRelation: this.formList[4].initialValue,
          contactRemark: this.formList[5].initialValue,
          isMain: 10041002,
        }
        this.$emit('contactHandle', params);
        this.closeDialog('refresh');
      } else {
        this.$message.info('请填写必填项!');
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
  }
};
</script>

<style lang="less" scoped>
</style>
